package com.knowif.backend.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * @author ：Xh
 * @date ：Created in 2019/8/25 0025 16:58
 */
public class AliyunMessageUtil {

    public static final String REGIONID = "华东2（上海）";
    public static final String ACCESSKEYID = "LTAIY03uXPK1kYzO";
    public static final String SECRET = "4dLZ0AN4OhRNAckJESsCa4ws3jthEG";
    public static final String templateACode = "SMS_172887663";
    public static final String SignName = "知识鸭";

    /**
     * 阿里云发送短信验证码
     * @param code
     * @param phoneNum
     */
    public static void sendVerificationCode(String code, String phoneNum) {

        JSONObject json = new JSONObject();
        json.put("code", code);

        DefaultProfile profile = DefaultProfile.getProfile(REGIONID, ACCESSKEYID, SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.setMethod(MethodType.POST);
        request.putQueryParameter("RegionId", REGIONID);
        request.putQueryParameter("PhoneNumbers", phoneNum);
        request.putQueryParameter("SignName", SignName);
        request.putQueryParameter("TemplateCode", templateACode);
        request.putQueryParameter("TemplateParam", json.toJSONString());
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
